package com.zzyl.nursing.controller.member;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 预约信息Controller
 *
 * @author Katana
 * @date 2025-05-23
 */
@RestController
@RequestMapping("/member/reservation")
@Api(tags = "预约信息相关接口")
public class ReservationController extends BaseController {
    @Autowired
    private IReservationService reservationService;
    
    
    /**
     * 查询预约信息列表
     */
    @GetMapping("/page")
    @ApiOperation(value = "获取预约信息列表")
    public AjaxResult list(Reservation reservation) {
        startPage();
        List<Reservation> list = reservationService.selectReservationList(reservation);
        //为返回的list最外层中增加msg和code
        TableDataInfo dataTable = getDataTable(list);
        return AjaxResult.success(dataTable);
    }
    
    /**
     * 新增预约信息
     */
    @PostMapping
    @ApiOperation("新增预约信息")
    public AjaxResult add(@ApiParam(value = "预约信息实体", required = true) @RequestBody Reservation reservation) {
        return toAjax(reservationService.insertReservation(reservation));
    }
    
    /**
     * 修改预约信息
     */
    
    @Log(title = "预约信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改预约信息")
    public AjaxResult edit(@ApiParam(value = "预约信息实体", required = true) @RequestBody Reservation reservation) {
        return toAjax(reservationService.updateReservation(reservation));
    }
    
    
    /**
     * 查询当天取消预约数量
     */
    @GetMapping("/cancelled-count")
    public AjaxResult cancelledCount() {
        //1 获取当天开始时间和结束时间
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        //2 获取当前线程中的用户ID
        Long userId = UserThreadLocal.getUserId();
        
        return AjaxResult.success(reservationService.cancelledCount(startTime, endTime, userId));
    }
    
    /**
     * 查询每个时间段的剩余的预约数
     */
    @GetMapping("/countByTime")
    @ApiOperation("查询当天每个时间段剩余的预约数")
    public AjaxResult countByTime(Long time) {
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        return AjaxResult.success(reservationService.countByTime(startTime));
    }
    
    /**
     * 取消预约
     */
    @PutMapping("/{id}/cancel")
    @ApiOperation("取消预约")
    public AjaxResult cancel(@ApiParam(value = "预约ID", required = true) @PathVariable Long id) {
        return toAjax(reservationService.cancelReservation(id));
    }
}
